{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "turned-hunger",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "lyric-toner",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html>\n",
      " <head>\n",
      "  <title>\n",
      "   This is a python demo page\n",
      "  </title>\n",
      " </head>\n",
      " <body>\n",
      "  <p class=\"title\">\n",
      "   <b>\n",
      "    The demo python introduces several python courses.\n",
      "   </b>\n",
      "  </p>\n",
      "  <p class=\"course\">\n",
      "   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n",
      "   <a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">\n",
      "    Basic Python\n",
      "   </a>\n",
      "   and\n",
      "   <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">\n",
      "    Advanced Python\n",
      "   </a>\n",
      "   .\n",
      "  </p>\n",
      " </body>\n",
      "</html>\n"
     ]
    }
   ],
   "source": [
    "demo = requests.get('http://python123.io/ws/demo.html').text\n",
    "# print(demo)\n",
    "soup = BeautifulSoup(demo, 'html.parser')\n",
    "print(soup.prettify())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "greatest-pathology",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<title>This is a python demo page</title>\n"
     ]
    }
   ],
   "source": [
    "print(soup.title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "economic-navigation",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>\n"
     ]
    }
   ],
   "source": [
    "print(soup.a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "american-fortune",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "print(soup.a.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "processed-payroll",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p\n"
     ]
    }
   ],
   "source": [
    "print(soup.a.parent.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "small-product",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "body\n"
     ]
    }
   ],
   "source": [
    "print(soup.a.parent.parent.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "enormous-neighbor",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}\n"
     ]
    }
   ],
   "source": [
    "tag = soup.a\n",
    "print(tag.attrs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "promotional-teacher",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Basic Python\n"
     ]
    }
   ],
   "source": [
    "print(soup.a.string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "blocked-worker",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The demo python introduces several python courses.\n"
     ]
    }
   ],
   "source": [
    "print(soup.p.string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "handmade-science",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
      "\n",
      "\n",
      "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for child in soup.body.children:\n",
    "    print(child)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "likely-direction",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<body>\n",
      "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
      "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
      "</body>\n"
     ]
    }
   ],
   "source": [
    "print(soup.body)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "waiting-intervention",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
      "<b>The demo python introduces several python courses.</b>\n",
      "The demo python introduces several python courses.\n",
      "\n",
      "\n",
      "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
      "Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>\n",
      "Basic Python\n",
      " and \n",
      "<a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>\n",
      "Advanced Python\n",
      ".\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for child in soup.body.descendants:\n",
    "    print(child)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "excessive-grain",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html><head><title>This is a python demo page</title></head>\n",
      "<body>\n",
      "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
      "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
      "</body></html>\n"
     ]
    }
   ],
   "source": [
    "soup = BeautifulSoup(demo, 'html.parser')\n",
    "print(soup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "excess-anniversary",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(soup.a.previous_sibling)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "lyric-issue",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">\n",
      " Basic Python\n",
      "</a>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(soup.a.prettify())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "extraordinary-dominant",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>\n"
     ]
    }
   ],
   "source": [
    "print(soup.a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "prostate-concord",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
